/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package models.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.List;
import models.entity.Oferta;
import utilidades.DataBaseInstance;

/**
 *
 * @author hmardones
 */
public class OfertaDao {
    
     public List<Oferta> getOferta(){
        List<Oferta> listaOfertas = new LinkedList<Oferta>();
        ResultSet result = null;
     try {
            VehiculoDao v=new VehiculoDao();
            String query = "select "
                                + "ID_OFERTA,"
                                + "PATENTE,"
                                + "MODELO,"
                                + "DESCRIPCION_OFERTA,"
                                + "PRECIO "
                            + "from "
                                + "OFERTAS";
            Connection conn = DataBaseInstance.getInstanceConnection();
            Statement stmt = conn.createStatement();
            result = stmt.executeQuery(query);
            Oferta oferta;
            
            while (result.next()) {               
                oferta=new Oferta();
                oferta.setId_oferta(result.getInt("ID_OFERTA"));                
                oferta.setVehiculo(v.findById(result.getString("PATENTE")));
                oferta.setDescripcion_oferta(result.getString("DESCRIPCION_OFERTA"));
                oferta.setPrecio(result.getInt("PRECIO"));
                oferta.setModelo(result.getString("MODELO"));
                listaOfertas.add(oferta);
            }
            result.close();
            stmt.close();
            DataBaseInstance.closeConnection(conn);

        } catch (SQLException se) {
            System.out.println(se.toString());
            System.err.println("Se ha producido un error de BD.");
            System.err.println(se.getMessage());
        }
        return listaOfertas;
    }
     
}
